package DAO;

import DATA.Book;
import DATA.Comment;
import DATA.User;
import DButils.ConnectTool;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.*;
import java.util.List;
public class BookDAO {
    public static List<Book> FindbyName(String name) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="select * from books where bookName = ?";
        List<Book> ans= (List<Book>) utils.find(sql,new BeanListHandler(Book.class),name);
        return ans;
    }

    public static Book FindbyID(int bid) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="select * from books where id = ?";
        Book ans= (Book) utils.find(sql,new BeanHandler(Book.class),bid);
        return ans;
    }
    public static List<Book> FindbyAuthor(String name){

        return null;
    }
    public static List<Book>FindAll() throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="select * from books";
        List<Book> ans= (List<Book>) utils.find(sql,new BeanListHandler(Book.class),null);
        return ans;
    }
    public static boolean modify(String col,String val){
        return false;
    }
    public static boolean insertBook(String bookName, String bookType, String author) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="insert into books ( bookName,bookType,author ) values (?,?,?)";
        boolean ans=utils.insert(sql,new BeanHandler(Book.class),bookName,bookType,author);
        return ans;
    }
    public static boolean deleteBook(Book target){
        return false;
    }

    public static Book findByBookNameAndAuthor(String bookName,String author) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="select * from books where bookName = ? and author = ?";
        Book ans=(Book) utils.find(sql,new BeanHandler(Book.class),bookName,author);
        return ans;
    }
}
